<?php
namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\ActType;
use App\Models\Game;
use App\Models\GameGoods;
use App\Models\GamePrize;
use App\Models\GameType;
use Illuminate\Http\Request;
use App\User;

class GameListController extends CommonController
{
    /**
     *获取列表
     */
    public function getIndex(Request $request)
    {
        $data = $request->input();

        $lists = Game::where('activity_id', $data['id'])->get()->toArray();
        if (!count($lists)) {
            return $this->tip('该活动下一个游戏都没有， 请先添加第一个游戏', url('game/index'));
        }
        $lists = Game::with('type')->where('activity_id', $data['id'])->paginate(10);

        $actions = User::getPermissionAction(5121,1);
        return view('gamelist.index')->with("lists", $lists)->with("actions", $actions)->with("activity_id", $data['id']);
    }

    /**
     * 获取创建页面
     */
    public function getCreate(Request $request)
    {
        $data = $request->input();
        $activity = Activity::where('id', $data['activity_id'])->first();
        $types  =GameType::get();
        $goods = GameGoods::get();

        return view('gamelist.create')->with('activity', $activity)->with('types', $types)->with('goods', $goods);
    }

    /**
     * 执行一个创建
     */
    public function postCreate(Request $request)
    {
        $data = $request->input();
        //判断是否有重复的特等奖，一等奖，二等奖，及三等奖
        $prize_types = array();
        $prize_goods = array();
        $prize_numbers = array();

        foreach($data as $k=>$v){
            if(strpos($k,'prize_type')!==false){
                $prize_types[] = $v;
            }
            if(strpos($k,'prize_goods')!==false){
                $prize_goods[] = $v;
            }
            if(strpos($k,'number')!==false){
                $prize_numbers[] = $v;
            }
        }

        foreach($prize_numbers as $k=>$v){
            if(!is_int($v+0) || ($v+0)<1){
                return AjaxCallbackMessage('至少存在一个奖品的数量不为正整数，请更正再提交',false);
            }
        }

        $times = array_count_values($prize_types);
//        dd($times);
        if(isset($times[0]) && $times[0]>1){
            return AjaxCallbackMessage('特等奖只能设置一个,请更正后，再提交',false);
        }


        if(isset($times[1]) && $times[1]>1){
            return AjaxCallbackMessage('一等奖只能设置一个,请更正后，再提交',false);
        }


        if(isset($times[2]) && $times[2]>1){
            return AjaxCallbackMessage('二等奖只能设置一个,请更正后，再提交',false);
        }

        if(isset($times[3]) && $times[3]>1){
            return AjaxCallbackMessage('三等奖只能设置一个,请更正后，再提交',false);
        }

        //判断一个游戏下面的奖项是否有重复的
        if(count($prize_goods)!=count(array_flip($prize_goods))){
            return AjaxCallbackMessage('一个游戏下面的奖品物品，不允许重复，请检查',false);
        }

        //判断是否提交了空的奖品物品
        if(in_array('', $prize_goods)){
            return AjaxCallbackMessage('至少存在一个奖品物品是，未选择，请更正再提交',false);
        }

        //判断是否提交了空奖品类型
        if(in_array('', $prize_types)){
            return AjaxCallbackMessage('至少存在一个奖品类型是，未选择，请更正再提交',false);
        }
        $aId = $data['activity_id'];

        $list = Game::create(['activity_id'=>$data['activity_id'], 'type_id'=>$data['game_type'], 'title'=>$data['game_name']]);
        unset($data['activity_id'],$data['game_type'],$data['game_name'], $data['formType'], $data['_token']);
//        dd($data);
        $total = count($data);
        for($i=0;$i<$total/4;$i++){
            $arr = array_values(array_slice($data, $i*4, 4));
//            dd($arr);
            GamePrize::create(['game_id'=>$list->id, 'title'=>$arr[0], 'type'=>$arr[1], 'goods_id'=>$arr[2], 'num'=>$arr[3], 'left'=>$arr[3]]);
            //库存做相应的减少
            GameGoods::where('id', $arr[2])->decrement('stock', $arr[3]);;
        }


        return AjaxCallbackMessage('游戏创建成功', true, url('gamelist/index?id='.$aId));
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $game = Game::where('id', $data['id'])->first();
        $activity = Activity::where('id', $game->activity_id)->first();
        $types  =GameType::get();
        $prizes = GamePrize::where('game_id', $game->id)->orderBy('type', 'asc')->get();
//        dd($prizes);

        //把当前奖项的数量设置还原到物品当中去
        $lists = array();
        foreach($prizes as $k=>$v){
            $stock = GameGoods::where('id', $v->goods_id)->first();
//            dd($v->goods_id);
            $stock = $stock->stock+$v->num;
            $v->stock =$stock;
            $lists[] = $v;
        }
        $arr = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五');
        $goods = GameGoods::get();
//        dd($prizes);

        return view('gamelist.edit')->with('types', $types)->with('goods', $goods)->with('game', $game)
            ->with('activity', $activity)->with('lists', $lists)->with('arr', $arr);
    }

    /**
     *执行一个编辑
     */
    public function postEdit(Request $request)
    {
        $data = $request->input();

        $game = Game::where('id', $data['id'])->first();
        if(0==$game->status){
            return AjaxCallbackMessage('该游戏已经使用了,不允许编辑', false);
        }

        //判断是否有重复的特等奖，一等奖，二等奖，及三等奖
        $prize_types = array();
        $prize_goods = array();
        $prize_numbers = array();

        foreach($data as $k=>$v){
            if(strpos($k,'prize_type')!==false){
                $prize_types[] = $v;
            }
            if(strpos($k,'prize_goods')!==false){
                $prize_goods[] = $v;
            }
            if(strpos($k,'number')!==false){
                $prize_numbers[] = $v;
            }
        }

        foreach($prize_numbers as $k=>$v){
            if(!is_int($v+0) || ($v+0)<1){
                return AjaxCallbackMessage('至少存在一个奖品的数量不为正整数，请更正再提交',false);
            }
        }

        $times = array_count_values($prize_types);
        if(isset($times[0]) && $times[0]>1){
            return AjaxCallbackMessage('特等奖只能设置一个,请更正后，再提交',false);
        }


        if(isset($times[1]) && $times[1]>1){
            return AjaxCallbackMessage('一等奖只能设置一个,请更正后，再提交',false);
        }


        if(isset($times[2]) && $times[2]>1){
            return AjaxCallbackMessage('二等奖只能设置一个,请更正后，再提交',false);
        }

        if(isset($times[3]) && $times[3]>1){
            return AjaxCallbackMessage('三等奖只能设置一个,请更正后，再提交',false);
        }

        //判断一个游戏下面的奖项是否有重复的
        if(count($prize_goods)!=count(array_flip($prize_goods))){
            return AjaxCallbackMessage('一个游戏下面的奖品物品，不允许重复，请检查',false);
        }

        //判断是否提交了空的奖品物品
        if(in_array('', $prize_goods)){
            return AjaxCallbackMessage('至少存在一个奖品物品是，未选择，请更正再提交',false);
        }

        //判断是否提交了空奖品类型
        if(in_array('', $prize_types)){
            return AjaxCallbackMessage('至少存在一个奖品类型是，未选择，请更正再提交',false);
        }

        Game::where('id', $data['id'])->update(['type_id'=>$data['game_type'], 'title'=>$data['game_name']]);
        $prizes = GamePrize::where('game_id', $data['id'])->get();

        foreach($prizes as $k=>$v){
            //库存做相应增加
            GameGoods::where('id', $v->goods_id)->increment('stock', $v->num);
        }
        GamePrize::where('game_id', $data['id'])->delete();
        $id = $data['id'];
        unset($data['activity_id'],$data['game_type'],$data['game_name'], $data['formType'], $data['_token'],$data['id']);
//        dd($data);
        $total = count($data);
        for($i=0;$i<$total/4;$i++){
            $arr = array_values(array_slice($data, $i*4, 4));
            GamePrize::create(['game_id'=>$id,'title'=>$arr[0], 'type'=>$arr[1], 'goods_id'=>$arr[2], 'num'=>$arr[3],'left'=>$arr[3]]);
            //库存做相应的减少
            GameGoods::where('id', $arr[2])->decrement('stock', $arr[3]);;
        }

        return AjaxCallbackMessage('游戏编辑成功', true, url('gamelist/index?id='.$game->activity_id));
    }

    /**
     * 执行一个删除
     */
    public function postDelete(Request $request)
    {
        $data = $request->input();
        $game = Game::where('id', $data['gid'])->first();
        if(0==$game->status){
            return AjaxCallbackMessage('该游戏已经使用了,不允许删除', false);
        }

        //恢复该游戏下的库存
        $prizes = GamePrize::where('game_id', $data['gid'])->get();
        foreach($prizes as $k=>$v){
            GameGoods::where('id', $v->goods_id)->increment('stock', $v->num);
        }

        Game::where('id', $data['gid'])->delete();
        GamePrize::where('game_id', $data['gid'])->delete();

        return AjaxCallbackMessage('删除成功，该游戏下的奖项也一起删除了，奖品库存已经恢复', true);
    }
}